//
//  TVCardLinkedView.swift
//  StarTimesON
//
//  Created by Toj on 8/24/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit

enum TVCardStyleType {
    case small
    case big
    
    var named: String {
        switch self {
        case .big:
            return "commodity_tv_card_bgImg_has"
        default:
            return "commodity_tv_card_bgImg_has_small"
        }
    }
}

class TVCardLinkedView: UIView {
    
    var styleType: TVCardStyleType = .big { didSet {
        backgroundImageView.image = styleType.named.to_image
        tipsLabel.isHidden = styleType == .small
    } }
    
    var model: TVCardCellModel? { didSet {
        guard let card = model else { return }
        // 卡号
        cardNumberLabel.text = card.cardNumber.st_numberFormat()
        
        // 卡状态
        stateLabel.text = card.stateString
        cardStateIcon.image = card.cardState.icon.to_image
        // 卡的提示信息
        tipsLabel.text = card.tips
        
        // 信号来源
        dthSignalModeView.model = card.products[DVBTVPlatForm.dth.rawValue]
        dttSignalModeView.model = card.products[DVBTVPlatForm.dtt.rawValue]
        
        relayoutSubviews()
    } }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        setupMakeLayoutSubviews()
    }
    
    // 背景图片
    private lazy var backgroundImageView: UIImageView = {
        let view = UIImageView()
        view.image = TVCardStyleType.big.named.to_image
        return view
    }()
    
    // 卡号标题
    private lazy var cardTitleLabel: UILabel = {
        let view = UILabel()
        view.text = "smart_card_no_content".localized()
        view.textColor = .white
        view.font = UIFont.boldSystemFont(ofSize: 16.f_scale)
        return view
    }()
    
    // 卡号
    private lazy var cardNumberLabel: UILabel = {
        let view = UILabel()
        view.textColor = .white
        view.font = UIFont.boldSystemFont(ofSize: 24.f_scale)
        return view
    }()
    
    // 其他
    lazy var unlinkButton: UIButton = {
        let view = UIButton()
        view.setImage(UIImage(named:"commodity_tv_card_unlink"), for: .normal)
        return view
    }()
    
    // dth信号模式视图
    private lazy var dthSignalModeView = TVSignalModeView()
    
    // dtt信号模式视图
    private lazy var dttSignalModeView = TVSignalModeView()
    
    // 卡状态图标
    private lazy var cardStateIcon: UIImageView = {
        let view = UIImageView()
        return view
    }()
    
    // 卡状态说明
    private lazy var stateLabel: UILabel = {
        let view = UILabel()
        view.textColor = .white
        view.font = UIFont.boldSystemFont(ofSize: 14.f_scale)
        return view
    }()
    
    // 卡提示
    private lazy var tipsLabel: UILabel = {
        let view = UILabel()
        view.numberOfLines = 0
        view.textColor = .white
        view.font = UIFont.boldSystemFont(ofSize: 12.f_scale)
        return view
    }()
    private let defaultWidth = (STScreenWidth - 32.ui_scale) * 0.5
}

// MARK: - 初始化
extension TVCardLinkedView {
    
    func setupMakeAddSubviews() {
        
        addSubview(backgroundImageView)
        addSubview(cardTitleLabel)
        addSubview(cardNumberLabel)
        addSubview(unlinkButton)
        addSubview(dthSignalModeView)
        addSubview(dttSignalModeView)
        addSubview(cardStateIcon)
        addSubview(stateLabel)
        addSubview(tipsLabel)
    }
    
    private func setupMakeLayoutSubviews() {
        
        backgroundImageView.snp.makeConstraints { make in
            make.edges.equalTo(self)
        }
        
        // 卡number标题
        cardTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(12.ui_scale)
            make.top.equalTo(21.ui_scale)
            make.height.equalTo(16.ui_scale)
        }
        
        cardNumberLabel.snp.makeConstraints { make in
            make.bottom.equalTo(cardTitleLabel)
            make.left.equalTo(cardTitleLabel.snp.right).offset(4.ui_scale)
        }
        
        unlinkButton.snp.makeConstraints { make in
            make.centerY.equalTo(cardTitleLabel)
            make.right.equalTo(snp.right).offset(-12.ui_scale)
            make.width.equalTo(24.ui_scale)
            make.height.equalTo(24.ui_scale)
        }
        
        // 卡状态图标
        cardStateIcon.snp.makeConstraints { make in
            make.top.equalTo(cardTitleLabel.snp.bottom).offset(47.ui_scale)
            make.left.equalTo(cardTitleLabel)
            make.width.equalTo(20.ui_scale)
            make.height.equalTo(20.ui_scale)
        }
        
        stateLabel.snp.makeConstraints { make in
            make.centerY.equalTo(cardStateIcon)
            make.left.equalTo(cardStateIcon.snp.right).offset(4.ui_scale)
        }
        
        tipsLabel.snp.makeConstraints { make in
            make.left.equalTo(cardStateIcon)
            make.right.equalTo(snp.right).offset(-12.ui_scale)
            make.top.equalTo(stateLabel.snp.bottom)
            make.bottom.equalTo(self)
        }
        
        relayoutSubviews()
    }
    
    private func relayoutSubviews() {
        
        dthSignalModeView.isHidden = dthSignalModeView.model == nil
        dttSignalModeView.isHidden = dttSignalModeView.model == nil
        let font = UIFont.boldSystemFont(ofSize: 14.ui_scale)
        var width = dthSignalModeView.model?.nameWidth(font: font) ?? defaultWidth
        // view内出了名称以外的宽度
        let assist: CGFloat = 44.ui_scale // 24 + 4 + 8 + 8
        if !dthSignalModeView.isHidden {
            width += assist
        }
        dthSignalModeView.snp.remakeConstraints { make in
            make.top.equalTo(50.ui_scale)
            make.left.equalTo(cardTitleLabel)
            make.width.equalTo(width)
            make.height.equalTo(24.ui_scale)
        }
        
        // 27 = 12 + 15
        let left = dthSignalModeView.isHidden ? 12.ui_scale : (width + 27.ui_scale)
        width = dttSignalModeView.model?.nameWidth(font: font) ?? defaultWidth
        if !dttSignalModeView.isHidden {
            // 80 = 12 + assist + 12 + 12
            let maxWidth = STScreenWidth - left - 80.f_scale
            if width > maxWidth { width = maxWidth }
            width += assist
        }
        dttSignalModeView.snp.remakeConstraints { make in
            make.top.equalTo(50.ui_scale)
            make.left.equalTo(left)
            make.width.equalTo(width)
            make.height.equalTo(24.ui_scale)
        }
    }
}
